//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   COUNTRY
// AUTHOR           :   Craig Chun
// DATE CREATE      :   2007/07/12
// DESCRIPTION      :   a MAINTENANCE FORM file for the function, COUNTRY
//                  :
//                  :
//                  :
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/07/12   Craig Chun      1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.setup.country.web;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import com.erp.framework.controller.form.*;
import com.erp.framework.controller.form.SimpleMaintStyleFormTemplate;
import com.erp.framework.model.schema.MetaData;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;
import com.erp.app.admin.errorMsg.pk.ErrorMsgLabelPk;
import com.erp.app.admin.errorMsg.vo.ErrorMsgLabelVo;
import com.erp.app.common.ProjectConstants;
import com.erp.app.common.ProjectErrMsgConstants;
import com.erp.app.common.select.beans.SelectDelegate;
import com.erp.app.setup.codeSetup.pk.CodeHdrPk;
import com.erp.app.util.web.*;

/**
* <p>A <strong>MaintenanceForm</strong> is a JavaBean which provides property
* getter and setter methods for all of the bean properties, and implements
* <code>Serializable</code>, as required by the JavaBean specification
* The main purpose for this bean is to get the user input
* from the web browsers.
*
* @author          Craig Chun
* @version 1.0     2007/07/12
* @since           FrameWork 1.0
*
*/
public class CountryMaintenanceForm extends SimpleMaintStyleFormTemplate {

	//Header Attributes (DB Table Name: T_COUNTRY_HDR)
	private String companyCode = "";                                                                   //DB Column Name: COMPANY_CODE
	private String countryCode = "";                                                                   //DB Column Name: COUNTRY_CODE
	private String hdrCountryName = "";                                                                //DB Column Name: COUNTRY_NAME
	private String hdrIsoCountryCode = "";                                                             //DB Column Name: ISO_COUNTRY_CODE
	private String hdrCountryAbbr = "";                                                                //DB Column Name: COUNTRY_ABBR
	private String hdrStatus = "";                                                                     //DB Column Name: STATUS
	private String createUser = "";                                                                    //DB Column Name: CREATE_USER
	private String createDate = "";                                                                    //DB Column Name: CREATE_DATE
	private String updateUser = "";                                                                    //DB Column Name: UPDATE_USER
	private String updateDate = "";                                                                    //DB Column Name: UPDATE_DATE

	//Child Attributes (DB Table Name: T_COUNTRY_PORT)
	private String[] portCode;                                                                         //DB Column Name: PORT_CODE
	private String[] portPortName;                                                                     //DB Column Name: PORT_NAME
	private String[] portIsoPortCode;                                                                  //DB Column Name: ISO_PORT_CODE
	private String[] portPortType;                                                                     //DB Column Name: PORT_TYPE
	private String[] portPortAbbr;                                                                     //DB Column Name: PORT_ABBR
	private String[] portStatus;                                                                       //DB Column Name: STATUS
	private String[] portSeq;		                                                                   //DB Column Name: SEQ

	/**
	* Validate the properties that have been set for this non-HTTP request,
	* and return an <code>ActionErrors</code> object that encapsulates any
	* validation errors that have been found.  If no errors are found, return
	* <code>null</code> or an <code>ActionErrors</code> object with no
	* recorded error messages.
	* <p>
	* The default implementation attempts to forward to the HTTP version of
	* this method.
	*
	* @param      mapping      the action mapping
	* @param      formIn       the action form
	* @param      request      the http request
	* @param      response     the http response
	* @param      errors       the action errors array
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	public void moreValidate(ActionMapping mapping, ActionForm formIn,
		HttpServletRequest request, HttpServletResponse response, ActionErrors errors) {
		final CountryMaintenanceForm form = (CountryMaintenanceForm) formIn;
		BeanValidator validator = new BeanValidator();
		validator.validate(mapping, form, request, response, errors);
		validator.validateMultiRows(mapping, form, request, response, errors, form.getMultiRows(CountryMaintenanceAction.TAB_IDX_PORT),
			new MultiRowValidator() {

				public String getLineNumber(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
						HttpServletResponse response, ActionErrors errors, MultiRow row) {
					return ((CountryPortRow)row).getPortCode();
				}

				public void moreValidate(ActionMapping mapping, ActionForm formIn, HttpServletRequest request,
							HttpServletResponse response, ActionErrors errors, MultiRow row) {
					// More Validation Logic Here. By default this method be invoked if the errors.size() == 0.
					ArrayList portList = form.getMultiRows(CountryMaintenanceAction.TAB_IDX_PORT);
					CountryPortRow cpRow = null;
					HashMap hm = new HashMap();
					SelectDelegate sdg = new SelectDelegate();

					String where = " COMPANY_CODE='" + form.getCompanyCode() + "'"
								+ " AND COUNTRY_CODE<>'" + form.getCountryCode() + "'";

					for(int i = 0 ; i < portList.size(); i++){
                		cpRow = (CountryPortRow)portList.get(i);
                		if (cpRow.isDeleted()) continue;
            			String country = CommonUtil.nullToString(sdg.findField("T_COUNTRY_PORT","COUNTRY_CODE",where + " AND PORT_CODE='"+ CommonUtil.nullToString(cpRow.getPortCode()) + "' "));
            			if(country.trim().length() > 0){
            				ActionErrorUtil.addErrorForSpecficPurpose(errors,cpRow.getPortCode() + " : " + ((ErrorMsgLabelVo) form.getErrMsgMap().get(form.getErrorPk(ProjectErrMsgConstants.MOD_COUNTRY_PORT_CODE_HAS_DEFINED_ERROR))).getObjectWebLabel() + country );
            			}
                		if(!hm.containsKey(cpRow.getPortCode())){
                			hm.put(cpRow.getPortCode(),cpRow);
                		}else{
                			ActionErrorUtil.addErrorForSpecficPurpose(errors,((ErrorMsgLabelVo) form.getErrMsgMap().get(form.getErrorPk(ProjectErrMsgConstants.MOD_COUNTRY_DUPLICATE_PORT_CODE_ERROR))).getObjectWebLabel());
                		}
                	}

				}

			}.setTabLabelObjectId("LBL_TAB_PORT")
		);

	}

	/**
	* Converts to a string representing the data in this object.
	* A new <code>String</code> object is allocated and initialized to
	* contain the character sequence currently represented by this
	* object. This <code>String</code> is then returned. Subsequent
	* changes to this object do not affect the contents of the
	* <code>String</code>.
	*
	* @return     a string representation of the object.
	* @see        java.lang.Object#toString()
	* @author     Craig Chun
	* @since      Framework 1.0
	*/
	public String toString() {
		String pkStr = "PK: {companyCode = " + this.companyCode
			+ "\n, countryCode = " + this.countryCode + "}\n";
		String colStr = "COL: {hdrCountryName = " + this.hdrCountryName
			+ "\n, hdrIsoCountryCode = " + this.hdrIsoCountryCode
			+ "\n, hdrCountryAbbr = " + this.hdrCountryAbbr
			+ "\n, hdrStatus = " + this.hdrStatus
			+ "\n, createUser = " + this.createUser
			+ "\n, createDate = " + this.createDate
			+ "\n, updateUser = " + this.updateUser
			+ "\n, updateDate = " + this.updateDate + "}\n";
		return "CountryMaintenanceForm.toString() (header only) is:\n" + pkStr + colStr;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Header (CountryHdr)
	////////////////////////////////////////////////////////////////////////////////

	public String getCompanyCode() {
		return this.companyCode;
	}

	public String getCountryCode() {
		return this.countryCode;
	}

	public String getHdrCountryName() {
		return this.hdrCountryName;
	}

	public String getHdrIsoCountryCode() {
		return this.hdrIsoCountryCode;
	}

	public String getHdrCountryAbbr() {
		return this.hdrCountryAbbr;
	}

	public String getHdrStatus() {
		return this.hdrStatus;
	}

	public String getCreateUser() {
		return this.createUser;
	}

	public String getCreateDate() {
		return this.createDate;
	}

	public String getUpdateUser() {
		return this.updateUser;
	}

	public String getUpdateDate() {
		return this.updateDate;
	}

	public void setCompanyCode(String companyCode) {
		this.companyCode = CommonUtil.toCode(companyCode);
	}

	public void setCountryCode(String countryCode) {
		this.countryCode = CommonUtil.toCode(countryCode);
	}

	public void setHdrCountryName(String hdrCountryName) {
		this.hdrCountryName = CommonUtil.trim(hdrCountryName);
	}

	public void setHdrIsoCountryCode(String hdrIsoCountryCode) {
		this.hdrIsoCountryCode = CommonUtil.trim(hdrIsoCountryCode);
	}

	public void setHdrCountryAbbr(String hdrCountryAbbr) {
		this.hdrCountryAbbr = CommonUtil.trim(hdrCountryAbbr);
	}

	public void setHdrStatus(String hdrStatus) {
		this.hdrStatus = CommonUtil.trim(hdrStatus);
	}

	public void setCreateUser(String createUser) {
		this.createUser = CommonUtil.trim(createUser);
	}

	public void setCreateDate(String createDate) {
		this.createDate = CommonUtil.trim(createDate);
	}

	public void setUpdateUser(String updateUser) {
		this.updateUser = CommonUtil.trim(updateUser);
	}

	public void setUpdateDate(String updateDate) {
		this.updateDate = CommonUtil.trim(updateDate);
	}

	////////////////////////////////////////////////////////////////////////////////
	//Generated Accessors (get/set methods) for Child (CountryPort)
	////////////////////////////////////////////////////////////////////////////////

	public String[] getPortCode() {
		return this.portCode;
	}

	public String[] getPortPortName() {
		return this.portPortName;
	}

	public String[] getPortIsoPortCode() {
		return this.portIsoPortCode;
	}

	public String[] getPortPortType() {
		return this.portPortType;
	}

	public String[] getPortPortAbbr() {
		return this.portPortAbbr;
	}

	public String[] getPortStatus() {
		return this.portStatus;
	}

	public void setPortCode(String[] portCode) {
		this.portCode = portCode;
	}

	public void setPortPortName(String[] portPortName) {
		this.portPortName = portPortName;
	}

	public void setPortIsoPortCode(String[] portIsoPortCode) {
		this.portIsoPortCode = portIsoPortCode;
	}

	public void setPortPortType(String[] portPortType) {
		this.portPortType = portPortType;
	}

	public void setPortPortAbbr(String[] portPortAbbr) {
		this.portPortAbbr = portPortAbbr;
	}

	public void setPortStatus(String[] portStatus) {
		this.portStatus = portStatus;
	}

	public String[] getPortSeq() {
		return portSeq;
	}

	public void setPortSeq(String[] portSeq) {
		this.portSeq = portSeq;
	}

	////////////////////////////////////////////////////////////////////////////////
	//Other Accessors (get/set methods)
	////////////////////////////////////////////////////////////////////////////////
	public void beforeRenderField(Object bean, MetaData metadata, FieldUIControl control) {
        if (bean instanceof CountryPortRow) {
            CountryPortRow row = (CountryPortRow) bean;
            if (!ProjectConstants.ROW_MODE_CREATE.equals(row.getRowMode())) {
                if ("portCode".equals(control.getProperty())) {
                    control.setVisibility(ProjectConstants.UI_VISIBILITY_DISPLAYONLY);
                }
            }
        }
	}

	public Iterator getHdrIsoCountryCodeValues() {
		return this.getTemplate().getCodeDetailsValues(new CodeHdrPk(this.getCompanyCode(), ProjectConstants.CD_ISO_COUNTRY_CODE));
	}

	public ErrorMsgLabelPk getErrorPk(String errorObjectID) {
        return new ErrorMsgLabelPk(getCompanyCode(), DBMENU_GENERAL_MENU, getModuleName(),
                errorObjectID, getSysLangCode());
    }

}